<template>
  <view class="webview-container">
    <!-- 顶部导航栏 -->
    <view class="webview-header" v-if="showHeader">
      <view class="header-left" @click="handleBack">
        <uni-icons type="arrowleft" size="24" color="#333"></uni-icons>
        <text v-if="headerTitle">{{ headerTitle }}</text>
      </view>
      <view class="header-title">{{ currentTitle || '加载中...' }}</view>
      <view class="header-right">
        <slot name="header-right"></slot>
      </view>
    </view>
    
    <!-- 加载进度条 -->
    <view class="progress-bar" v-if="showProgress && progress < 100">
      <view class="progress" :style="{ width: progress + '%' }"></view>
    </view>
    
    <!-- WebView 组件 -->
    <web-view 
      :src="url" 
      :webview-styles="webviewStyles"
      @message="handleMessage"
      @onPostMessage="handlePostMessage"
      @load="handleLoad"
      @error="handleError"
      ref="webview"
    ></web-view>
    
    <!-- 错误提示 -->
    <view class="error-message" v-if="error">
      <text>{{ error }}</text>
      <button @click="reload" class="reload-btn">重新加载</button>
    </view>
  </view>
</template>

<script>
export default {
  name: 'UniWebView',
  props: {
    // 初始URL
    url: {
      type: String,
      required: true
    },
    // 是否显示顶部导航栏
    showHeader: {
      type: Boolean,
      default: true
    },
    // 自定义导航栏标题
    headerTitle: {
      type: String,
      default: ''
    },
    // 是否显示加载进度条
    showProgress: {
      type: Boolean,
      default: true
    },
    // WebView样式配置
    webviewStyles: {
      type: Object,
      default: () => ({
        progress: {
          color: '#1890ff'
        }
      })
    }
  },
  data() {
    return {
      currentTitle: '',
      progress: 0,
      error: null,
      canGoBack: false
    };
  },
  methods: {
    // 加载完成
    handleLoad(e) {
      this.progress = 100;
      this.error = null;
      this.$emit('load', e);
      
      // 获取页面标题
      // this.getPageTitle();
    },
    
    // 加载错误
    handleError(e) {
      this.error = '加载失败，请检查网络或URL是否正确';
      this.$emit('error', e);
    },
    
    // 接收消息
    handleMessage(e) {
      this.$emit('message', e.detail);
    },
    
    // 接收PostMessage
    handlePostMessage(e) {
      this.$emit('postmessage', e.detail);
    },
    
    // 重新加载
    reload() {
      this.error = null;
      this.progress = 0;
      this.$refs.webview.reload();
    },
    
    // 返回上一页
    handleBack() {
      if (this.canGoBack) {
        this.$refs.webview.back();
      } else {
        uni.navigateBack();
      }
    },
    
    // 获取页面标题
    getPageTitle() {
      // 注意：在H5端可以直接获取title，但在App端需要通过evalJS与网页通信获取
      // #ifdef H5
      // this.currentTitle = document.title;
      // #endif
      
      // #ifdef APP-PLUS
      // const currentWebview = this.$scope.$getAppWebview();
      // setTimeout(() => {
      //   currentWebview.evalJS('document.title').then(title => {
      //     this.currentTitle = title;
      //   });
      // }, 500);
      // #endif
    },
    
    // 向WebView发送消息
    postMessage(data) {
      // #ifdef APP-PLUS
      // const currentWebview = this.$scope.$getAppWebview();
      // currentWebview.evalJS(`window.postMessage(${JSON.stringify(data)}, '*')`);
      // #else
      // this.$refs.webview.postMessage(data);
      // #endif
    },
    
    // 更新WebView URL
    updateUrl(newUrl) {
      this.url = newUrl;
      this.progress = 0;
      this.error = null;
    }
  },
  watch: {
    url(newVal) {
      this.updateUrl(newVal);
    }
  }
};
</script>

<style scoped>
.webview-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100%;
}

.webview-header {
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 15px;
  background-color: #ffffff;
  border-bottom: 1px solid #f0f0f0;
}

.header-left {
  display: flex;
  align-items: center;
  min-width: 80px;
}

.header-title {
  flex: 1;
  text-align: center;
  font-size: 16px;
  color: #333;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 0 10px;
}

.header-right {
  min-width: 80px;
  display: flex;
  justify-content: flex-end;
}

.progress-bar {
  height: 2px;
  width: 100%;
  background-color: #f0f0f0;
}

.progress {
  height: 100%;
  background-color: #1890ff;
  transition: width 0.3s ease;
}

.error-message {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
  text-align: center;
  padding: 20px;
  color: #ff4d4f;
}

.reload-btn {
  margin-top: 15px;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  padding: 5px 15px;
}
</style>